* Firm organization with multiple establishments
* Section V: Table VII, Appendix Tables D.3, D.6, D.8, D.10, D.14-D.16, D.18-D.19, D.22-D.23 
* Bottom panel: Non-directly affected establishments

clear all
set matsize 2000
set more off

capture log close
log using log/20c_train_layer-firm_est-indirect.log, replace

/*
Contents: Results for non-directly affected establishments

	Table VII: Lower travel times affect all units of ME firms
	Table D.3: summary statistics

	Robustness to alternative weighting, matching, statistical inference
		Table D.6: Table VII unweighted
		Table D.8: Table VII without matching
		Table D.10: Table VII SE clustered at firm-county-level
		Table D.14: Table VII with alternative strata for non-directly affected establishments
		
	Robustness to alternative definitions of the main variables
		Table D.15: Table VII with additional outcome variables (firms with at least two establishments)
				log total number of employees; 
				average log wages of all employees;
				managerial span, defined as ratio of the number of employees over number of employees in the top layer (as in Friedrich 2020); 
				share of managerial occupations in wage sum in percent, where managerial occupations are determined by layer;
				ed. 0: education of non-managerial employees;
				ed. all: education of all employees;
				ex. 0: average log experience (= # of days in establishment/HQ) of non-managerial employees;
				ex. all: average log experience of all employees
		Table D.16: Table VII with stricter and broader treatment definition
		Table D.18: Table VII with Bundesland x year FE

	Robustness to alternative sample restrictions
		Table D.19: Table VII without route HH-Berlin
		Table D.22: Table VII w/o moving establishments
		Table D.23: Table VII with only always connected stations	

	Note: For coding reasons, the results for Tables D.3, D.6, D.10, D.15 are generated along with those for Table VII.
*/
		
use data/train_analysis_firm.dta, clear
desc

//	All years ICE connected
merge m:1 ao_kreis using data/AlwaysICE_ao.dta, keepusing(stetsICE)
drop _merge

replace stetsICE = 1 if ao_kreis == 2000 // Berlin
rename stetsICE ao_stetsICE

merge m:1 hq_kreis using data/AlwaysICE.dta, keepusing(stetsICE)
drop _merge

rename stetsICE hq_stetsICE
replace hq_stetsICE = 1 if hq_kreis == 2000
rename ao_stetsICE stetsICE

gen aux_ICE = (stetsICE == 1) if hauptbet == 0 & d_traindata == 1
bys untid: egen est_stetsICE = min(aux_ICE)

//	Bundesland x year FE
cap drop bula
gen bula = int(ao_kreis / 1000)
gen double bula_year = bula * 10000 + jahr

cap drop hq_bula
gen hq_bula = int(hq_kreis / 1000)
gen double hq_bula_year = hq_bula * 10000 + jahr
 
//	Broader treatment definition
gen aux_jahr = jahr if faster1 == 1
tab aux_jahr, m
bys betnr: egen treat_year_est = min(aux_jahr)
drop aux_jahr

preserve

keep untid ao_kreis ever_faster1 treat_year_est 
rename ao_kreis ptnr_cty
rename treat_year_est ptnr_treat_year
sort untid ptnr_cty
count
duplicates drop
count
save data/firm_county.dta, replace

restore

preserve

keep untid ao_kreis
duplicates drop
sort untid ao_kreis
count
joinby untid using data/firm_county.dta
drop if ao_kreis == ptnr_cty 
duplicates report untid ao_kreis ptnr_cty

count

rename ptnr_cty hq_kreis
sort hq_kreis ao_kreis
merge m:1 hq_kreis ao_kreis using data/Distanzen_v2014.dta
drop if _merge == 2
replace distance_all = . if _merge == 1
drop _merge
rename hq_kreis ptnr_cty
keep untid ao_kreis ptnr_cty distance_all ever_faster1 ptnr_treat_year

bys untid ao_kreis: egen min_dist = min(distance_all)
keep if distance_all == min_dist
count if min_dist == .
keep untid ao_kreis ptnr_cty min_dist ever_faster1 ptnr_treat_year
rename ever_faster1 ptnr_ever_faster1

describe
duplicates report untid ao_kreis 

drop if min_dist == .
bys untid ao_kreis: egen max_treat = max(ptnr_ever_faster1)
drop if ptnr_ever_faster1 == 0 & max_treat == 1

duplicates report untid ao_kreis 
duplicates drop untid ao_kreis, force
tabstat min_dist, s(n mean sd p5 q p95)
tabstat ptnr_ever_faster1, s(n mean sd p5 q p95)

save data/est_shortest_distance.dta, replace

restore

********************************************************************************
***	OTHER ESTABLISHMENTS	****************************************************
********************************************************************************

********************************************************************************
***	BASELINE REGRESSION
*** Table VII, firms with at least two establishments, Table D.3, D.6, D.10, D.15

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)
	eststo vceunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): vceunw`j'
		qui estadd scalar count_kreis = e(K2): vceunw`j'
	eststo vceaou`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster untid ao_kreis)
		qui estadd scalar count_bet = e(K1): vceaou`j'
		qui estadd scalar count_kreis = e(K2): vceaou`j'

	local j = `j' + 1
}

qui reghdfe ln_prdt_bet firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)

********************************************************************************
***	Table D.3, Descriptive statistics

foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet firm_faster1 {
	tabstat `outcome' if e(sample), by(jahr) s(n mean sd p5 p10 q p90 p95)
}

********************************************************************************
*** Table VII, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.6, bottom right
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceunw1 vceunw2 vceunw3 vceunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

********************************************************************************
*** Table D.10, bottom right
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vceaou1 vceaou2 vceaou3 vceaou4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
*** Additional outcome variables

local j = 1
foreach outcome in ln_empl_bet avg_lnw_bet ln_span_bet_bf shr_mgmt_w_bet avg_d_educ_bot_bet avg_d_educ_bet avg_tage_bet_bot_bet avg_tage_bet_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo vcew`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): vcew`j'
		qui estadd scalar count_kreis = e(K2): vcew`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
***	Table D.15, bottom
esttab vcew1 vcew2 vcew3 vcew4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab vcew5 vcew6 vcew7 vcew8, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.8: results without matching, firms with at least two establishments

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

bys ao_kreis: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 > 0)
by  ao_kreis: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 > 0)

//	at least one treated and one untreated establishment per county
keep if tmp2 > 0 & tmp4 > 0
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .
	
	eststo kreunw`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1, absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_bet = e(K1): kreunw`j'
		qui estadd scalar count_kreis = e(K2): kreunw`j'

	local j = `j' + 1
}

********************************************************************************
***	Table D.8, bottom right
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab kreunw1 kreunw2 kreunw3 kreunw4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Table D.14: Different strata (county x size x year), firms with at least two establishments

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(ao_kreis perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys hq_kreis jahr: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  hq_kreis jahr: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*
bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys hq_kreis jahr: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  hq_kreis jahr: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)
tab2 sample touse

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample
drop sample
qui gen sample = e(sample)

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*
drop weight

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample
drop sample
qui gen sample = e(sample)

drop aux_weight 
drop n_treat_s n_ec_s
cap drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.14, left 
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.14: Variations on strata (HQ county x size x year), firms with at least two establishments

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(hq_kreis perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.14, right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.16: Stricter treatment definition (only connections without changes)

{
preserve
keep if hauptbet == 0 & ever_faster1_direct == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1_direct == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_direct == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1_direct == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1_direct == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_direct if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1_direct == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_direct == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1_direct == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1_direct == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_direct == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_direct == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_direct == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_direct == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_direct if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_direct == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_direct == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_direct if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_direct if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.16, bottom left
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Table D.16: Broader treatment definition (assign to treatment group if closest establishment is treated)
*** Thus, drop from group of indirectly affected establishments if closest establishment is treated

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

merge m:1 untid ao_kreis using data/est_shortest_distance.dta
drop if _merge == 2
drop _merge
cap drop flg_est
egen flg_est = tag(betnr)
tab ptnr_ever_faster1 if flg_est == 1
tab ptnr_ever_faster1 if flg_est == 1 & ptnr_cty != hq_kreis
count

drop if ptnr_ever_faster1 == 1 & ptnr_cty != hq_kreis

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.16, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.18: Bundesland-year FE, firms with at least two establishments

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr bula_year hq_bula_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr bula_year hq_bula_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr bula_year hq_bula_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr bula_year hq_bula_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.18, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
*** Table D.19: Drop connection HH-Berlin

{
preserve
keep if hauptbet == 0 & ever_faster1_rob == 0 & d_traindata == 1 & count_est > 2

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1_rob == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1_rob == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1_rob == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1_rob == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1_rob if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1_rob == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1_rob == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1_rob == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1_rob == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1_rob == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1_rob == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1_rob == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1_rob == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1_rob if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1_rob == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1_rob == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1_rob if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1_rob if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.19, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.6f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label

restore
}

********************************************************************************
***	Table D.22: Drop moving establishments/headquarters

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2 & issue_est == 0 & issue_est_haupt == 0

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.22, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

********************************************************************************
***	Table D.23: Stations connected to ICE network in all years

{
preserve
keep if hauptbet == 0 & ever_faster1 == 0 & d_traindata == 1 & count_est > 2 & stetsICE == 1

count
count if flg_est == 1

bys betnr: egen first_year = min(jahr)
keep if first_year < 2002

count
count if flg_est == 1

gen aux_empl = empl_bet if jahr >= 2000 & jahr < 2002
bys betnr: egen empl_2000 = mean(aux_empl)
drop aux_empl

forvalues q = 25(25)75 {
	qui egen empl_perc_`q' = pctile(empl_2000), p(`q')
}
qui gen perc_empl = 100 if empl_2000 < .
forval q = 75(-25)25 {
	qui replace perc_empl = `q' if empl_2000 <= empl_perc_`q' & empl_perc_`q' != .
}
drop empl_perc*

egen group_strata = group(perc_empl jahr)
bysort group_strata: egen tmp1 = sum(firm_ever_faster1 == 0)
gen tmp2 = (tmp1 >0 )
bysort group_strata: egen tmp3 = sum(firm_ever_faster1 == 1)
gen tmp4 = (tmp3 >0 )
bys ao_kreis: egen tmp1b = sum(firm_ever_faster1 == 0)
gen tmp1c = (tmp1b > 0)
by  ao_kreis: egen tmp3b = sum(firm_ever_faster1 == 1)
gen tmp3c = (tmp3b > 0)

cap drop aux_touse
gen aux_touse = (tmp2 > 0 & tmp4 > 0 & tmp1c > 0 & tmp3c > 0)

cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if aux_touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop weight*

bys group_strata: egen tmp7 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp8 = (tmp7 > 0)
by  group_strata: egen tmp9 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp10 = (tmp9 > 0)
bys ao_kreis: egen tmp11 = sum(firm_ever_faster1 == 0 & sample == 1)
gen tmp12 = (tmp11 > 0)
by  ao_kreis: egen tmp13 = sum(firm_ever_faster1 == 1 & sample == 1)
gen tmp14 = (tmp13 > 0)

gen touse = (tmp8 > 0 & tmp10 > 0 & tmp12 > 0 & tmp14 > 0)
cap drop sample
qui reghdfe ln_empl_bet firm_faster1 if touse == 1, absorb(betnr county_year hqcounty_year) vce(robust)
qui gen sample = e(sample)

cap drop n_treat_s n_ec_s
bysort group_strata: egen n_treat_s = sum(firm_ever_faster1 == 1 & sample == 1)
bysort group_strata: egen n_ec_s = sum(firm_ever_faster1 == 0 & sample == 1)

by  group_strata: egen n_all_s = sum(sample == 1)
gen share_treated = n_treat_s/n_all_s
drop n_all_s share_treated

gen aux_weight = (n_treat_s/n_ec_s) if firm_ever_faster1 == 0 & aux_touse == 1 & sample == 1
replace aux_weight = 1 if firm_ever_faster1 == 1 & aux_touse == 1 & sample == 1

sum firm_ever_faster1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
local n_treat = `r(N)'

sum firm_ever_faster1 if firm_ever_faster1 == 0 & touse == 1 & sample == 1
local n_ec = `r(N)' 

gen weight = 1 if firm_ever_faster1 == 1 & touse == 1 & sample == 1
replace weight = `n_ec'/`n_treat'*aux_weight if firm_ever_faster1 == 0 & touse == 1 & sample == 1

tabstat weight if touse == 1 & sample == 1, c(s) s(N sum)

qui reghdfe ln_empl_bet firm_faster1 if touse == 1 & sample == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(robust)
tabstat weight if e(sample), c(s) s(N sum)
tab2 touse sample

drop aux_weight 
drop n_treat_s n_ec_s
drop tmp*

local j = 1
foreach outcome in ln_prdt_bet avg_lnw_bot_bet count_mgmt_bet shr_bloss_w_bet {
	
	qui sum `outcome', det
	qui replace `outcome' = r(p99) if `outcome' > r(p99) & `outcome' != .
	qui sum `outcome', det
	qui replace `outcome' = r(p1) if `outcome' < r(p1) & `outcome' != .

	eststo base`j', title(`outcome'): ///
		qui reghdfe `outcome' firm_faster1 if touse == 1 [aweight=weight], absorb(betnr county_year hqcounty_year) vce(cluster ao_kreis)
		qui estadd scalar count_hq = e(K1): base`j'
		qui estadd scalar count_kreis = e(K2): base`j'
		tabstat weight if e(sample), c(s) s(N sum)

	local j = `j' + 1
}

********************************************************************************
*** Table D.23, bottom right
esttab base1 base2 base3 base4, ///
	b(%9.3f) se(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
esttab base1 base2 base3 base4, ///
	b(%9.3f) p(%9.3f) star r2 obslast compress mtitles star(+ 0.20 ++ 0.10 * 0.05 ** 0.01 *** 0.001) stats(r2 N count_bet count_kreis, fmt(%9.3f %9.0f %9.0f %9.0f))  style(tex) label
	
restore
}

log close
